Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema:[],
    formData:{
      path: '/packageAPI/pages/miniprogramJump/miniprogramJump?zh=wx.restartMiniProgram&title=重启当前小程序&webUrl=https://developers.weixin.qq.com/miniprogram/dev/api/navigate/wx.restartMiniProgram.html',
      url: 'https://mp.weixin.qq.com/s/9O6fTsFrMkp5SDL8kYniiw'
    },
    title: '',
    result: '',
    remark: '',
    webUrl: '',
    apiName: '',
  },
  onLoad(options) {
    console.log('options', options)
    wx.setNavigationBarTitle({
      title: options.title,
    })
    
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
    if(options.zh === 'wx.restartMiniProgram') {
      this.setData({
        schema: [
          {
            label:'打开的页面路径，path 中 ? 后面的部分会成为 query',
            prop:'path',
            type:'text'
          }
        ]
      })
    }
    if(options.zh === 'wx.openOfficialAccountArticle') {
      this.setData({
        schema: [
          {
            label:'需要打开的公众号地址',
            prop:'url',
            type:'text'
          }
        ]
      })
    }
  },
  getFormData(e) {
    this.setData({
      formData: e.detail.formData
    })
  },
  restartMiniProgram(e) {
    const that = this;
    wx.restartMiniProgram({
      path: this.data.formData.path,
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  openOfficialAccountProfile(e) {
    const that = this;
    wx.openOfficialAccountProfile({
      username: 'gh_68046249a341',
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  openOfficialAccountArticle(e) {
    const that = this;
    wx.openOfficialAccountArticle({
      url: this.data.formData.url,
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  exitMiniProgram(e) {
    const that = this;
    wx.exitMiniProgram({
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  }
})